package com.ljm.travels.controller;

import com.ljm.travels.domain.Province;
import com.ljm.travels.domain.Result;
import com.ljm.travels.service.ProvinceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/province")
public class ProvinceController {
    @Autowired
    private ProvinceService provinceService;

    @PostMapping("/update")
    private Result update(@RequestBody Province province){
        Result result = new Result();
        try{
            provinceService.update(province);
            result.setMsg("更新省份信息成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setState(false).setMsg("更新省份信息失败");
        }
        return result;
    }

    /**
     * 根据id查询省份信息
     * @param id
     * @return
     */
    @GetMapping("/findOne")
    private Province findOne(Integer id){
        return provinceService.findOne(id);
    }

    @GetMapping("/delete")
    private Result deleteProvince(Integer id){
        Result result = new Result();
//        System.out.println(id);
        try {
            provinceService.delete(id);
            result.setMsg("删除成功！！！");
        }catch (Exception e){
            e.printStackTrace();
            result.setState(false).setMsg("删除信息失败！！！");
        }
        return result;
    }


    /***
     * 保存省份信息
     * @param province
     * @return
     */
    @PostMapping("/addProvince")
    private Result addProvince(@RequestBody Province province){
        Result result = new Result();
        log.info(province.toString());
        try {
            provinceService.save(province);
            result.setMsg("保存省份信息成功！！！");
        }catch (Exception e){
            e.printStackTrace();
            result.setState(false).setMsg("保存省份信息失败！！");
        }
        return result;
    }

    /**
     *分页查询所有
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("/findByPage")
    private Map<String,Object>findByPage(Integer page,Integer rows){
        //当page,rows为空时,赋值为1
        page=page==null?1:page;
        rows=rows==null?4:rows;

        HashMap<String, Object> map = new HashMap<>();
        //分页处理
        List<Province> provinces = provinceService.findByPage(page, rows);
        //计算总页数
        Long totals=provinceService.findTotals();//获取总条数

        Long totalPage=totals%rows==0?(totals/rows):(totals/rows)+1;//总页数

        map.put("provinces",provinces);
        map.put("totals",totals);
        map.put("totalPage",totalPage);
        map.put("page",page);
        return map;
    }

    @GetMapping("/findAll")
    private Map<String,Object> findAll(){

        List<Province>provinces=provinceService.findAll();
        Map<String,Object>result=new HashMap<>();
        result.put("provinces",provinces);
        return result;
    }
}
